<?php

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

class RcbaseModelMotorList extends JModel
{
	function getMotorList()
	{
		$db =& JFactory::getDBO();
		$limitstart    = JRequest::getInt('limitstart',0);
		$limit         = JRequest::getInt('limit',25);
		$motor_filtr   = JRequest::getVar('motor_filtr');
		$search_data   = array('count' => false, 'form'=>0);

		if ($motor_filtr){
			$filtr = array();
			foreach (array('kv', 'weight', 'max_current', 'max_voltage') as $p){
				$v = JRequest::getVar($p);
				if ($v){
					$search_data['param'][$p] = preg_replace('/[^0-9\-\.]/', '',$v);
					list($from,$to) = explode('-',$v);
					if ($to){
						array_push($filtr, "$p >= $from AND $p <= $to");
					} else {
						array_push($filtr, "$p = $v");
					}
				}
			}
			$search_data['param']['title'] = JRequest::getVar('title');
			if ($search_data['param']['title']){
				array_push($filtr, "title LIKE '%".$search_data['param']['title']."%'");
			}
		}
		if ($filtr){
			$search_data['sql'] = "
				SELECT SQL_CALC_FOUND_ROWS *
				FROM #__rcbase_motorlist
				WHERE 1 AND ".implode(' AND ',$filtr)."
				ORDER BY title, weight, max_current LIMIT ".$limitstart.", ".$limit."
			";
			$search_data['filtr'] = $filtr;
		} else {
			$search_data['sql'] = "SELECT SQL_CALC_FOUND_ROWS * FROM #__rcbase_motorlist ORDER BY title, weight, max_current LIMIT ".$limitstart.", ".$limit."";
		}
		$db->setQuery( $search_data['sql'] );
		$search_data['result'] = $db->loadAssocList();
		$db->setQuery('SELECT FOUND_ROWS();');
		$search_data['count']  = $db->loadResult();
		return $search_data;
	}

	function getMotorListCount(){
		$db =& JFactory::getDBO();
		$db->setQuery('SELECT FOUND_ROWS();');
		return $db->loadResult();
	}

	function getSearchResult()
	{
		$db   =& JFactory::getDBO();
		$user =& JFactory::getUser();
		$prop_submit   = JRequest::getVar('prop_submit');
		$model_submit  = JRequest::getVar('model_submit');
		$param_submit  = JRequest::getVar('param_submit');
		$limitstart    = JRequest::getInt('limitstart',0);
		$limit         = JRequest::getInt('limit',5);

		$search_data   = array('count' => false, 'form'=>0);
		if ($motor_filtr){
			foreach (array('title', 'kv', 'weight', 'current', 'voltage') as $p){
				$v = JRequest::getInt($p);
				$search_data['param'][$p] = preg_replace('/[^0-9\-\.]/', '',$v);
			}
			$search_data['sql'] = "

			";
		} elseif ($prop_submit){
			$search_data['form'] = 2;
			foreach (array('prop_d', 'prop_h') as $p){
				$search_data['param'][$p] = JRequest::getInt($p);
			}
			$search_data['sql'] = "
				SELECT t.id test_id, motor_id, t.user_id test_user_id, prop_type, prop_d, prop_h, cells, current, voltage, t.power, t.rate_up test_rate_up, t.rate_down test_rate_down,
				m.user_id motor_user_id, product_id, title, kv, weight, max_current, max_voltage, m.rate_up motor_rate_up, m.rate_down motor_rate_down
				FROM jos_rcbase_motortest t
				JOIN jos_rcbase_motorlist m ON m.id = t.motor_id
				WHERE ROUND(t.prop_d) = '".$search_data['param']['prop_d']."'
				AND ROUND(t.prop_h) = '".$search_data['param']['prop_h']."'
				ORDER BY m.weight, m.id, t.cells, t.power DESC
				LIMIT ".$limitstart.", ".$limit."
			";
		} elseif ($model_submit){
			$search_data['form'] = 1;
			foreach (array('model_type', 'weight') as $p){
				$search_data['param'][$p] = JRequest::getInt($p);
			}
			$model_type_filtr = array(
				'1' => "t.power BETWEEN ".(0.8 * $search_data['param']['weight'])." AND ".(1.0 * $search_data['param']['weight'])."",
				'2' => "t.power BETWEEN ".(1.0 * $search_data['param']['weight'])." AND ".(1.2 * $search_data['param']['weight'])."",
				'3' => "t.power BETWEEN ".(1.2 * $search_data['param']['weight'])." AND ".(1.5 * $search_data['param']['weight'])."",
				'4' => "t.power BETWEEN ".(1.5 * $search_data['param']['weight'])." AND ".(2.0 * $search_data['param']['weight'])."",
			);
			$search_data['sql'] = "
				SELECT t.id test_id, motor_id, t.user_id test_user_id, prop_type, prop_d, prop_h, cells, current, voltage, t.power, t.rate_up test_rate_up, t.rate_down test_rate_down,
				m.user_id motor_user_id, product_id, title, kv, weight, max_current, max_voltage, m.rate_up motor_rate_up, m.rate_down motor_rate_down
				FROM jos_rcbase_motortest t
				JOIN jos_rcbase_motorlist m ON m.id = t.motor_id
				WHERE ".$model_type_filtr[$search_data['param']['model_type']]."
				ORDER BY m.weight, t.cells, t.power DESC
				LIMIT ".$limitstart.", ".$limit."
			";
		} elseif ($param_submit){
			$search_data['form'] = 0;
			foreach (array('weight', 'kv', 'cells', 'current', 'power') as $p){
				$search_data['param'][$p.'_from'] = JRequest::getInt($p.'_from');
				$search_data['param'][$p.'_to'] = JRequest::getInt($p.'_to');
			}
			$search_data['sql'] = "
				SELECT SQL_CALC_FOUND_ROWS
				t.id test_id, motor_id, t.user_id test_user_id, prop_type, prop_d, prop_h, cells, current, voltage, t.power, t.rate_up test_rate_up, t.rate_down test_rate_down,
				m.user_id motor_user_id, product_id, title, kv, weight, max_current, max_voltage, m.rate_up motor_rate_up, m.rate_down motor_rate_down
				FROM jos_rcbase_motortest t
				JOIN jos_rcbase_motorlist m ON m.id = t.motor_id
				WHERE
				    m.kv >= '".     ($search_data['param']['kv_from']      ? $search_data['param']['kv_from'] : '0')."'
				AND m.kv <= '".     ($search_data['param']['kv_to']        ? $search_data['param']['kv_to'] : '9999')."'
				AND m.weight >= '". ($search_data['param']['weight_from']  ? $search_data['param']['weight_from'] : '0')."'
				AND m.weight <= '". ($search_data['param']['weight_to']    ? $search_data['param']['weight_to'] : '9999')."'
				AND t.cells >= '".  ($search_data['param']['cells_from']   ? $search_data['param']['cells_from'] : '0')."'
				AND t.cells <= '".  ($search_data['param']['cells_to']     ? $search_data['param']['cells_to'] : '9')."'
				AND t.current >= '".($search_data['param']['current_from'] ? $search_data['param']['current_from'] : '0')."'
				AND t.current <= '".($search_data['param']['current_to']   ? $search_data['param']['current_to'] : '999')."'
				AND t.power >= '".  ($search_data['param']['power_from']   ? $search_data['param']['power_from'] : '0')."'
				AND t.power <= '".  ($search_data['param']['power_to']     ? $search_data['param']['power_to'] : '9999')."'
				ORDER BY m.weight, m.id, t.cells, t.power DESC
				LIMIT ".$limitstart.", ".$limit."
			";
		} else {
			return $search_data;
		}
		$db->setQuery( $search_data['sql'] );
		$search_data['result'] = $db->loadAssocList();
		$db->setQuery('SELECT FOUND_ROWS();');
		$search_data['count']  = $db->loadResult();
		return $search_data;
	}

	function getMotorTester($id)
	{
		$db =& JFactory::getDBO();
		$query = '
			SELECT DISTINCT user_id, username
			FROM #__rcbase_motortest t
			JOIN #__users u ON u.id = t.user_id
			WHERE t.motor_id = '.$id;
		$db->setQuery( $query );
		$res = $db->loadAssocList('user_id');
		return $res;
	}


}